package com.wei.service;

import com.spring.BeanPostProcessor;
import com.spring.annotation.Component;
import com.spring.annotation.WeiValue;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * @author Althumic Wei
 * @date 2023-07-30 23:12
 **/
@Slf4j
@Component
public class WeiValueBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if (bean instanceof UserService) {
            for (Field field : bean.getClass().getDeclaredFields()) {
                if (field.isAnnotationPresent(WeiValue.class)) {
                    field.setAccessible(true);
                    try {
                        field.set(bean, field.getAnnotation(WeiValue.class).value());
                    } catch (IllegalAccessException e) {
                        log.error("field set value exception: ", e);
                    }
                }
            }
        }
        return bean;
    }
}
